<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-7-21
 * Func   购彩大厅
 */
defined('IN_YuLin') || exit('NO PERMIT!');

UserLogin(U($m.'/'.$c.'/'.$a));

$table = Table('lottery_period');
$ytable = Table('lottery_period_yilou');
$ytable2 = Table('lottery_period_yilou2');
$lptable = Table('lottery_play');

//	彩票种类
$data = [];
$id = intval($_GET['id']);
!$id && $id = intval($_GET['gameid']);
!$id && $id = intval($_GET['gameID']);
if($id)
{
	$data = $lottery[$id];
}

if($a == 'order')	//	我的订单
{
	$iid = intval($_GET['periodId']);
	!$iid && $iid = intval($_GET['periodid']);
	!$iid && $iid = intval($_GET['id']);
	
	$list = ['status'=>1,'list'=>[]];
	$list['list'] = $db->getall('SELECT prize,dateline FROM '.Table('lottery_order_list').' WHERE cid = '.$data['id'].' AND iid = '.$iid.' AND uid = '.$_uid.' AND status != 3 ORDER BY id DESC ');
	foreach($list['list'] as $key => $val)
	{
		$list['list'][$key]['date'] = date('Y-m-d',$val['dateline']);
	}

	echo json_encode($list);
	exit;
}
else if($a == 'orderlog')	//	历史订单
{
	$cid = intval($_GET['gameid']);
	!$cid && $cid = intval($_GET['id']);
	
	$num = max(1,intval($_GET['num']));
	
	$data = [];
	$tmp = $db->getall('SELECT l.*,p2.stoptime,p2.status AS PeriodStatus,p2.rednum AS result FROM '.Table('lottery_order_list').' l LEFT JOIN '.$table.' p2 ON l.iid = p2.id WHERE l.uid = '.$_uid.($cid ? ' AND p2.cid = '.$cid : '').' AND l.status != 3 ORDER BY l.id DESC LIMIT '.$num);
	foreach($tmp2 as $key => $val)
	{
		//	玩法
		$t2 = $db->getrow('SELECT name FROM '.$lptable.' WHERE playid = '.$val['pid'].' AND cid = '.$val['cid']);
		
		$tmp = [];
		$tmp['OrderId']			=	$val['id'];
		$tmp['Ordernumber']		=	$val['id'];
		$tmp['PeriodId']		=	$val['iid'];
		$tmp['NumberOfPeriod']	=	$val['period'];
		$tmp['GamePlayName']	=	$t2['name'];
		$tmp['CreateTime']		=	date('Y-m-d H:i:s',$val['dateline']);
		$tmp['Content']			=	$val['rednum'];
		$tmp['Times']			=	$val['times'];
		$tmp['MoneyModel']		=	$val['unit'];
		$tmp['BetNumber']		=	$val['multiple'];
		$tmp['Amount']			=	$val['money'];
		$tmp['KickBack']		=	0.0;
		$tmp['KickbackPercent']	=	0.0;//$val['KickbackPercent'];
		$tmp['KickbackAmount']	=	0.0;
		$tmp['OpenResult']		=	$val['result'];
		$tmp['BettingBalance']	=	bcadd($val['prize'],-$val['money']);
		$tmp['PeriodStatus']	=	$val['PeriodStatus'];	//	开奖状态	0未开始1已开始2开奖中3已开奖
		$tmp['OrderStatus']		=	$val['status'];	//	订单状态
		$tmp['IsTrack']			=	$val['tid'] ? true : false;
		$tmp['PeriodCloseTime']	=	date('Y-m-d H:i:s',$val['stoptime']);
		
		$data[] = $tmp;
	}
	
	echo json_encode($data);
	exit;
}
else if($a == 'orderpize')
{
	$cid = intval($_GET['gameid']);
	!$cid && $cid = intval($_GET['id']);
	
	$iid = intval($_GET['periodId']);
	!$iid && $iid = intval($_GET['periodid']);
	
	$where = ' AND l.prize > 0';
	$cid && $where .= ' AND p2.cid = '.$cid;
	$iid && $where .= ' AND l.iid = '.$iid;
	
	$num = max(1,intval($_GET['num']));
	
	$data = [];
	$tmp = $db->getall('SELECT l.*,p2.stoptime,p2.status AS PeriodStatus,p2.rednum AS result FROM '.Table('lottery_order_list').' l LEFT JOIN '.$table.' p2 ON l.iid = p2.id WHERE l.uid = '.$_uid.$where.' AND l.status != 3 ORDER BY l.id DESC LIMIT '.$num);
	foreach($tmp2 as $key => $val)
	{
		//	玩法
		$t2 = $db->getrow('SELECT name FROM '.$lptable.' WHERE playid = '.$val['pid'].' AND cid = '.$val['cid']);
		
		$tmp = [];
		$tmp['OrderId']			=	$val['id'];
		$tmp['Ordernumber']		=	$val['id'];
		$tmp['PeriodId']		=	$val['iid'];
		$tmp['NumberOfPeriod']	=	$val['period'];
		$tmp['GamePlayName']	=	$t2['name'];
		$tmp['CreateTime']		=	date('Y-m-d H:i:s',$val['dateline']);
		$tmp['Content']			=	$val['rednum'];
		$tmp['Times']			=	$val['times'];
		$tmp['MoneyModel']		=	$val['unit'];
		$tmp['BetNumber']		=	$val['multiple'];
		$tmp['Amount']			=	$val['money'];
		$tmp['KickBack']		=	0.0;
		$tmp['KickbackPercent']	=	0.0;//$val['KickbackPercent'];
		$tmp['KickbackAmount']	=	0.0;
		$tmp['OpenResult']		=	$val['result'];
		$tmp['BettingBalance']	=	bcadd($val['prize'],-$val['money']);
		$tmp['PeriodStatus']	=	$val['PeriodStatus'];	//	开奖状态	0未开始1已开始2开奖中3已开奖
		$tmp['OrderStatus']		=	$val['status'];	//	订单状态
		$tmp['IsTrack']			=	$val['tid'] ? true : false;
		$tmp['PeriodCloseTime']	=	date('Y-m-d H:i:s',$val['stoptime']);
		
		$data[] = $tmp;
	}
	
	echo json_encode($data);
	exit;
}
else if($a == 'orderview')	//	订单详情
{
	$id = intval($_GET['orderid']);
	!$id && AjaxReturn(1,'请选择要查看的订单');
	
	$data = $db->getrow('SELECT l.*,p2.rednum AS result FROM '.Table('lottery_order_list').' l LEFT JOIN '.$table.' p2 ON l.iid = p2.id WHERE l.uid = '.$_uid.' AND l.id = '.$id.' AND l.status != 3');
	!$data && AjaxReturn(1,'查看的订单信息不存在');
	
	//	玩法
	$t2 = $db->getrow('SELECT name FROM '.$lptable.' WHERE playid = '.$data['pid'].' AND cid = '.$data['cid']);
	$data['name'] = $t2['name'];
	
	//	彩票种类
	$data['title'] = $lottery[$data['cid']]['title'];
	
	//	{"status":true,"info":"成功！","result":"{\"order\":{\"FUserID\":644091,\"FAccount\":\"lsye6935\",\"FGameID\":26,\"FGameName\":\"重庆时时彩\",\"FGamePlayID\":1297,\"FGamePlayName\":\"五星复式\",\"FFirstAwardId\":20953,\"FOrderNumber\":\"73008251039323605066\",\"FCreateTime\":\"2017-08-25T10:39:32\",\"FTotalAmount\":2.0000,\"FTimes\":1,\"FMoneyModel\":1,\"FBetNumber\":1,\"FBettingBalance\":-2.0000,\"FTotalKickback\":0.0000,\"FKickbackPercent\":0.0,\"FPeriodID\":107620684,\"FNumberOfPeriod\":\"20170825028\",\"OpenResult\":\"2,7,5,8,8\",\"OrderStatus\":1,\"PeriodStatus\":4,\"FContent\":\"5|7|2|5|0\",\"FIsTrack\":false,\"FTrackId\":0},\"details\":[{\"FID\":150979165,\"FOrderID\":148528595,\"FGamePlayItemID\":20953,\"FAwardLevel\":1,\"FName\":\"复式\",\"FODDS\":198000.0000,\"FKickback\":0.0000,\"FStatus\":1,\"FRemark\":\"0\",\"FWinningCount\":0,\"FBettingBalance\":-2.0000}]}"}
	$res = [];
	$res['FuserID']		=	$_uid;
	$res['FAccount']	=	$_user['username'];
	$res['FGameID']		=	$data['cid'];
	$res['FGameName']	=	$data['title'];
	$res['FGamePlayID']	=	$data['pid'];
	$res['FGamePlayName']=	$data['name'];
	$res['FFirstAwardId']=	$data['pid'];	//	不清楚意义
	$res['FOrderNumber']=	$data['id'];
	$res['FCreateTime']	=	date('Y-m-d H:i:s',$data['dateline']);
	$res['FTotalAmount']=	$data['money'];
	$res['FTimes']		=	$data['times'];
	$res['FMoneyModel']	=	$data['unit'];
	$res['FBetNumber']	=	$data['multiple'];
	$res['FBettingBalance']	=	bcadd(-$data['money'],$data['prize']);
	$res['FTotalKickback']	=	0.0;	//	不清楚意义
	$res['FKickbackPercent']=	0.0;	//	不清楚意义
	$res['FPeriodID']		=	$data['iid'];
	$res['FNumberOfPeriod']	=	$data['period'];
	$res['OpenResult']		=	$data['result'];
	$res['OrderStatus']		=	$data['status'];
	$res['PeriodStatus']	=	$data['status'];	//	开奖状态
	$res['FContent']		=	$data['rednum'];
	$res['FIsTrack']		=	false;	//	追号
	$res['FTrackId']		=	0;	//	追号ID
	
	$res2 = [];
	$res2['FID']			=	$data['id'];	//	不清楚含义
	$res2['FOrderID']		=	$data['id'];
	$res2['FGamePlayItemID']=	$data['pid'];	//	对应api/lottery/play2中SI
	$res2['FAwardLevel']	=	1;				//	不清楚含义
	$res2['FName']			=	$data['name'];
	$res2['FODDS']			=	0;		//	本玩法最大奖
	$res2['FKickback']		=	0.0;	//	不清楚含义
	$res2['FStatus']		=	1;		//	不清楚含义
	$res2['FRemark']		=	0;
	$res2['FWinningCount']	=	0;
	$res2['FBettingBalance']=	$res['FBettingBalance'];
	
	AjaxReturn(0,'',['order'=>$res,'details'=>$res2]);
}
else if($a == 'log')	//	趋势
{
	!$data && $data = current($lottery);
	
	
	$tpl->display('index/lottery_log_'.$data['ename']);
}
else if($a == 'view')
{
	!$data && $data = current($lottery);
	
	//	最近开奖6期
	$lastperiod = ['total'=>0,'list'=>[]];
	$tmp = $db->getall('SELECT period,stoptime,rednum FROM '.$table.' WHERE cid = '.$data['id'].' AND rednum != \'\' ORDER BY stoptime DESC LIMIT 6');
	foreach($tmp as $val)
	{
		$lastperiod['list'][] = ['period'=>$val['period'],'stoptime','date'=>date('Y-m-d H:i:s',$val['stoptime']),'result'=>$val['rednum']];
	}
	$lastperiod['total'] = count($lastperiod['list']);
	
	//	上一期
	$lottery0 = $db->getrow('SELECT * FROM '.$table.' WHERE cid = '.$data['id'].' AND rednum != \'\' ORDER BY stoptime DESC');
	
	//	当前售卖期
	$lottery1 = $db->getrow('SELECT * FROM '.$table.' WHERE cid = '.$data['id'].' AND stoptime >= '.TIMESTAMP.' ORDER BY stoptime');
	
	//	下一期
	$lottery2 = $db->getrow('SELECT * FROM '.$table.' WHERE cid = '.$data['id'].' AND stoptime >= '.TIMESTAMP.' ORDER BY stoptime LIMIT 1,1');
	
	//	上一期、当前期、下一期
	$nowperiod = [];
	$nowperiod['fpreviousperiod']	=	$lottery0['period'];
	$nowperiod['fpreviousresult']	=	$lottery0['rednum'];
	$nowperiod['fid']				=	$lottery1['id'];
	$nowperiod['fnumberofperiod']	=	$lottery1['period'];
	$nowperiod['fstarttime']		=	date('Y/m/d H:i:s',$lottery1['starttime']);
	$nowperiod['flottostarttime']	=	date('Y/m/d H:i:s',$lottery1['stoptime']);
	$nowperiod['fstatus']			=	$lottery1['stoptime'] > TIMESTAMP ? 1 : 0;
	$nowperiod['fclosetime']		=	date('Y/m/d H:i:s',$tmp[1]['stoptime']);
	$nowperiod['fnextperiod']		=	$lottery2['period'];
	$nowperiod['fnextstarttime']	=	date('Y/m/d H:i:s',$lottery2['starttime']);
	$nowperiod['fsettlefid']		=	$lottery2['id'];
	$nowperiod['fsettlenumber']		=	$lottery0['period'];
	$nowperiod['fsettletime']		=	date('Y/m/d H:i:s',$lottery0['stoptime']);
	$nowperiod['ServerTime']		=	date('Y/m/d H:i:s');
	$nowperiod['fisstopseles']		=	$nowperiod['fstatus'] ? 'False' : 'True';
	
	//	今日未封盘期数
	$nextperiod = [];
	$tmp = $db->getall('SELECT id,period,starttime,stoptime,rednum FROM '.$table.' WHERE cid = '.$data['id'].' AND stoptime > '.TIMESTAMP.' AND day = \''.date('Ymd').'\' ORDER BY stoptime');
	foreach($tmp as $key => $val)
	{
		$status = $key ? 0 : 1;
		$nextperiod[] = ['PeriodId'=>$val['id'],'NumberOfPeriod'=>$val['period'],'StartTime'=>date('Y-m-d H:i:s',$val['starttime']),'CloseTime'=>date('Y-m-d H:i:s',$val['stoptime'] - 20),'Status'=>$status];
	}
	
	//	最近20个订单
	$lastorder = [];
	$tmp2 = $db->getall('SELECT l.*,p2.stoptime,p2.status AS PeriodStatus,p2.rednum AS result FROM '.Table('lottery_order_list').' l LEFT JOIN '.$table.' p2 ON l.iid = p2.id WHERE l.uid = '.$_uid.' AND l.cid = '.$data['id'].' AND l.status != 3 ORDER BY l.id DESC LIMIT 20');
	foreach($tmp2 as $key => $val)
	{
		//	玩法
		$t2 = $db->getrow('SELECT name FROM '.$lptable.' WHERE playid = '.$val['pid'].' AND cid = '.$val['cid']);
		
		$tmp = [];
		$tmp['OrderId']			=	$val['id'];
		$tmp['Ordernumber']		=	$val['id'];
		$tmp['PeriodId']		=	$val['iid'];
		$tmp['NumberOfPeriod']	=	$val['period'];
		$tmp['GamePlayName']	=	$t2['name'];
		$tmp['CreateTime']		=	date('Y-m-d H:i:s',$val['dateline']);
		$tmp['Content']			=	$val['rednum'];
		$tmp['Times']			=	$val['times'];
		$tmp['MoneyModel']		=	$val['unit'];
		$tmp['BetNumber']		=	$val['multiple'];
		$tmp['Amount']			=	$val['money'];
		$tmp['KickBack']		=	0.0;
		$tmp['KickbackPercent']	=	0.0;//$val['KickbackPercent'];
		$tmp['KickbackAmount']	=	0.0;
		$tmp['OpenResult']		=	$val['result'];
		$tmp['BettingBalance']	=	bcadd($val['prize'],-$val['money']);
		$tmp['PeriodStatus']	=	$val['PeriodStatus'];	//	开奖状态	0未开始1已开始2开奖中3已开奖
		$tmp['OrderStatus']		=	$val['status'];	//	订单状态
		$tmp['IsTrack']			=	$val['tid'] ? true : false;
		$tmp['PeriodCloseTime']	=	date('Y-m-d H:i:s',$val['stoptime']);
		
		$lastorder[] = $tmp;
	}
	
	if(!$data['credit'])
	{
		$tpl->display('index/lottery_view');
		exit;
	}
	$tpl->display('index/lottery_view_'.$data['ename']);
	//$tpl->display('index/lottery_view'.($data['credit'] ? '_credit' : ''));
	exit;
}